package com.hardy.demo.spring;

import lombok.Data;
import lombok.Getter;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;

import java.util.Set;

/**
 * Author: Hardy
 * Date:   2020/5/5
 * Description:
 **/
@Getter
public class SpringQualifier {

    @Data
    static class User {

        private String name;

        private int age;
    }

    @Bean
    public User user() {
        User user = new User();
        user.setName("1");
        user.setAge(1);
        return user;
    }

    @Bean
    @Qualifier
    public User user1() {
        User user = new User();
        user.setName("2");
        user.setAge(2);
        return user;
    }

    @Autowired
    private Set<User> users;

    @Autowired
    @Qualifier
    private Set<User> users1;

    public static void main(String[] args) {
        AnnotationConfigApplicationContext application = new AnnotationConfigApplicationContext(SpringQualifier.class);

        SpringQualifier springQualifier = application.getBean(SpringQualifier.class);

        System.out.println(springQualifier.getUsers());

        System.out.println(springQualifier.getUsers1());

        BeanFactory beanFactory = application.getDefaultListableBeanFactory();

        beanFactory.getBean(SpringQualifier.class);

        application.close();
    }
}
